package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.constructionwork.*;
import com.glink.manage.dto.person.PersonAccessMonadPageDTO;
import com.glink.manage.service.ConstructionWorkAccessMonadService;
import com.glink.manage.vo.constructionwork.ConstructionWorkAccessMonadDetailsVO;
import com.glink.manage.vo.constructionwork.ConstructionWorkAccessMonadPageVO;
import com.glink.manage.vo.constructionwork.ConstructionWorkListVO;
import com.glink.manage.vo.person.PersonAccessMonadPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author  qiushaoshan
 * @since  2025-01-08
 */
@Slf4j
@RestController
@Api(tags = "工程管理-准入申请单")
@RequestMapping("/API/Manage/constructionWorkAccessMonad")
public class ConstructionWorkAccessMonadController extends AbstractBaseController {

    @Autowired
    private ConstructionWorkAccessMonadService constructionWorkAccessMonadService;
    
    @ApiOperation(value = "01.新增准入申请")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/insertAccessMonad", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertAccessMonad(@Validated @RequestBody ConstructionWorkAccessMonadSaveDTO dto) throws GeneralSimpleException {
        log.info("01.新增准入申请：{}", LogUtil.encode(dto));
        boolean status = constructionWorkAccessMonadService.insertAccessMonad(dto);
        log.info("01.新增准入申请：success");
        return this.response(status);
    }

    @ApiOperation(value = "02.查询可办理准入的在建工程")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findWorkList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkListVO>> findWorkList(@Validated @RequestBody ConstructionWorkAccessMonadListDTO dto) throws GeneralSimpleException {
        log.info("02.查询可办理准入的在建工程：{}", LogUtil.encode(dto));
        List<ConstructionWorkListVO> workList = constructionWorkAccessMonadService.findWorkList(dto);
        log.info("02.查询可办理准入的在建工程：success");
        return this.response(workList);
    }

    @ApiOperation(value = "03.撤回申请单")
    @PostMapping(value = "/withdraw", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> withdraw(@Validated @RequestBody ConstructionWorkAccessMonadWithdrawDTO dto) throws GeneralSimpleException {
        log.info("03.撤回申请单：{}", LogUtil.encode(dto));
        Boolean status = constructionWorkAccessMonadService.withdraw(dto);
        log.info("03.撤回申请单：success");
        return this.response(status);
    }
    
    @ApiOperation(value = "04.同意审批")
    @PostMapping(value = "/agreeApprove", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> agreeApprove(@Validated @RequestBody ConstructionWorkAccessMonadAgreeDTO dto) throws GeneralSimpleException {
        log.info("04.同意审批：{}", LogUtil.encode(dto));
        Boolean status = constructionWorkAccessMonadService.agreeApprove(dto);
        log.info("04.同意审批：success");
        return this.response(status);
    }

    @ApiOperation(value = "05.驳回审批")
    @PostMapping(value = "/rejectApprove", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> rejectApprove(@Validated @RequestBody ConstructionWorkAccessMonadRejectDTO dto) throws GeneralSimpleException {
        log.info("05.驳回审批：{}", LogUtil.encode(dto));
        Boolean status = constructionWorkAccessMonadService.rejectApprove(dto);
        log.info("05.驳回审批：success");
        return this.response(status);
    }

    @ApiOperation(value = "06.查询审批单详情")
    @PostMapping(value = "/findById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<ConstructionWorkAccessMonadDetailsVO> findById(@Validated @RequestBody ConstructionWorkAccessMonadIdDTO dto) throws GeneralSimpleException {
        log.info("06.查询审批单详情：{}", LogUtil.encode(dto));
        ConstructionWorkAccessMonadDetailsVO detailsVO = constructionWorkAccessMonadService.findDetailById(dto);
        log.info("06.查询审批单详情：success");
        return this.response(detailsVO);
    }

    @ApiOperation(value = "07.分页查询审批单-待审批列表")
    @PostMapping(value = "/pageByPendingApproval", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<ConstructionWorkAccessMonadPageVO>> pageByPendingApproval(@Validated @RequestBody ConstructionWorkAccessMonadPageDTO dto) throws GeneralSimpleException {
        log.info("07.分页查询审批单-待审批列表：{}", LogUtil.encode(dto));
        IPage<ConstructionWorkAccessMonadPageVO> accessMonadPageVOIPage = constructionWorkAccessMonadService.pageByPendingApproval(dto);
        log.info("07.分页查询审批单-待审批列表：success");
        return this.response(accessMonadPageVOIPage);
    }

    @ApiOperation(value = "08.分页查询审批单-已审批列表")
    @PostMapping(value = "/pageByApproval", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<ConstructionWorkAccessMonadPageVO>> pageByApproval(@Validated @RequestBody ConstructionWorkAccessMonadPageDTO dto) throws GeneralSimpleException {
        log.info("08.分页查询审批单-已审批列表：{}", LogUtil.encode(dto));
        IPage<ConstructionWorkAccessMonadPageVO> accessMonadPageVOIPage = constructionWorkAccessMonadService.pageByApproval(dto);
        log.info("08.分页查询审批单-已审批列表：success");
        return this.response(accessMonadPageVOIPage);
    }

    @ApiOperation(value = "09.查询准入申请列表")
    @PostMapping(value = "/pageByAccessMonad", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<PersonAccessMonadPageVO>> pageByAccessMonad(@Validated @RequestBody PersonAccessMonadPageDTO dto) throws GeneralSimpleException {
        log.info("09.查询准入申请列表：{}", LogUtil.encode(dto));
        IPage<PersonAccessMonadPageVO> accessMonadPageVOIPage = constructionWorkAccessMonadService.pageByAccessMonad(dto);
        log.info("09.查询准入申请列表：success");
        return this.response(accessMonadPageVOIPage);
    }
}
